package com.project.movie.controller.cinema;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.*;
import com.project.movie.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 影院controller
 * @date 2025/02/06 02:47
 */
@Controller
@ResponseBody
@RequestMapping("cinema")
public class MovieCinemaController {

    @Autowired
    private MovieCinemaService movieCinemaService;
    @Autowired
    private MovieSliceService movieSliceService;
    @Autowired
    private MovieItemService movieItemService;
    @Autowired
    private MoviePerformerService moviePerformerService;

    /** 分页获取影院 */
    @PostMapping("getMovieCinemaPage")
    public Result getMovieCinemaPage(@RequestBody MovieCinema movieCinema) {
        Page<MovieCinema> page = new Page<>(movieCinema.getPageNumber(),movieCinema.getPageSize());
        QueryWrapper<MovieCinema> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieCinema.getUserId()),MovieCinema::getUserId,movieCinema.getUserId())
                .like(StringUtils.isNotBlank(movieCinema.getName()),MovieCinema::getName,movieCinema.getName())
                .like(StringUtils.isNotBlank(movieCinema.getAddress()),MovieCinema::getAddress,movieCinema.getAddress())
                .like(StringUtils.isNotBlank(movieCinema.getBrand()),MovieCinema::getBrand,movieCinema.getBrand())
                .like(StringUtils.isNotBlank(movieCinema.getProvince()),MovieCinema::getProvince,movieCinema.getProvince())
                .like(StringUtils.isNotBlank(movieCinema.getCity()),MovieCinema::getCity,movieCinema.getCity())
                .like(StringUtils.isNotBlank(movieCinema.getCounty()),MovieCinema::getCounty,movieCinema.getCounty())
                .like(StringUtils.isNotBlank(movieCinema.getTheater()),MovieCinema::getTheater,movieCinema.getTheater())
                .like(StringUtils.isNotBlank(movieCinema.getTel()),MovieCinema::getTel,movieCinema.getTel())
                .eq(movieCinema.getState() != null,MovieCinema::getState,movieCinema.getState());
        Page<MovieCinema> movieCinemaPage = movieCinemaService.page(page, queryWrapper);
        return Result.success(movieCinemaPage);
    }

    @GetMapping("getMovieByCinema")
    public Result getMovieByCinema(@RequestParam("id") String id) {
        QueryWrapper<MovieSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieSlice::getCinemaId,id).groupBy(MovieSlice::getItemId);
        List<MovieSlice> movieSliceList = movieSliceService.list(queryWrapper);
        List<MovieItem> list = new ArrayList<>();
        for (MovieSlice movieSlice : movieSliceList) {
            MovieItem movieItem = movieItemService.getById(movieSlice.getItemId());
            QueryWrapper<MoviePerformer> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MoviePerformer::getItemId,movieItem.getId());
            List<MoviePerformer> performerList = moviePerformerService.list(wrapper);
            List<String> name = new ArrayList<>();
            for (MoviePerformer moviePerformer : performerList) {
                name.add(moviePerformer.getName());
            }
            movieItem.setPerformer(StringUtils.join(name,","));
            list.add(movieItem);
        }
        return Result.success(list);
    }

    @GetMapping("getMovieCinemaList")
    public Result getMovieCinemaList() {
        List<MovieCinema> cinemaList = movieCinemaService.list();
        return Result.success(cinemaList);
    }

    @PostMapping("getMovieCinemaListSearch")
    public Result getMovieCinemaListSearch(@RequestBody MovieCinema movieCinema) {
        QueryWrapper<MovieCinema> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieCinema.getName()),MovieCinema::getName,movieCinema.getName())
                .like(StringUtils.isNotBlank(movieCinema.getAddress()),MovieCinema::getAddress,movieCinema.getAddress())
                .like(StringUtils.isNotBlank(movieCinema.getBrand()),MovieCinema::getBrand,movieCinema.getBrand())
                .like(StringUtils.isNotBlank(movieCinema.getProvince()),MovieCinema::getProvince,movieCinema.getProvince())
                .like(StringUtils.isNotBlank(movieCinema.getCity()),MovieCinema::getCity,movieCinema.getCity())
                .like(StringUtils.isNotBlank(movieCinema.getCounty()),MovieCinema::getCounty,movieCinema.getCounty())
                .like(StringUtils.isNotBlank(movieCinema.getTheater()),MovieCinema::getTheater,movieCinema.getTheater())
                .like(StringUtils.isNotBlank(movieCinema.getTel()),MovieCinema::getTel,movieCinema.getTel())
                .eq(movieCinema.getState() != null,MovieCinema::getState,movieCinema.getState());
        List<MovieCinema> movieCinemaPage = movieCinemaService.list(queryWrapper);
        return Result.success(movieCinemaPage);
    }

    /** 根据id获取影院 */
    @GetMapping("getMovieCinemaById")
    public Result getMovieCinemaById(@RequestParam("id")String id) {
        MovieCinema movieCinema = movieCinemaService.getById(id);
        return Result.success(movieCinema);
    }

    /** 保存影院 */
    @PostMapping("saveMovieCinema")
    public Result saveMovieCinema(@RequestBody MovieCinema movieCinema) {
        boolean save = movieCinemaService.save(movieCinema);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑影院 */
    @PostMapping("editMovieCinema")
    public Result editMovieCinema(@RequestBody MovieCinema movieCinema) {
        boolean save = movieCinemaService.updateById(movieCinema);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除影院 */
    @GetMapping("removeMovieCinema")
    public Result removeMovieCinema(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                QueryWrapper<MovieSlice> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MovieSlice::getCinemaId,id);
                int count = movieSliceService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("影院已排片,无法删除");
                }
                movieCinemaService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("影院id不能为空！");
        }
    }

}
